/**
 * Created by leman on 2017/7/18.
 */
'use strict';

salesCtrl.controller('proceedManageCtrl', ['$rootScope', '$scope', '$http', '$location', function ($rootScope, $scope, $http, $location) {
    //配置
    $scope.count = 1; //数据总数
    $scope.p_pernum = 10; //每页显示条数
    //变量
    $scope.p_current = 1;  //当前页数
    $scope.p_all_page = 0;  //总页数
    $scope.pages = [];
    $scope.consignorId =null;
    $scope.billCode =null;
    $scope.payeeStatus =null;
    $scope.billDateStart =null;
    $scope.billDateEnd =null;

    $("#consignorId").val(10);
    //初始化第一页
    getPage($scope.p_current, "");
    //搜索
    $scope.seaBtn = function () {
        getPage($scope.p_current, $scope.p_pernum, "")
    };

    function getPage(page, size,  callback) {
        // "xxx.html?status=0&page="+page+"&size="+size
        //查询
        // console.log($scope.consignorId )
        // console.log($scope.billCode )
        // console.log($scope.billStatus)
        // console.log($scope.billDateStart)
        // console.log($scope.billDateEnd)
        $http.post("receivable/rece-list", {
            "pageNo": page,
            "consignorId": $scope.consignorId ,
            "billCode": $scope.billCode,
            "payeeStatus": $scope.payeeStatus,
            //不显示未对账账单
            "unBillStatus": "10",
            "billDateStartString": $scope.billDateStart,
            "billDateEndString": $scope.billDateEnd
        }).success(function (res) {
            // var thisData= res.data.concat().splice(page*10-10, 10);//当前页数*每页显示条数-每页显示条数,每页显示条数
            $scope.optiondata=  res.data.cust;
            $scope.count = res.data.page.totalRecord;
            $scope.data = res.data;
            $scope.p_current = page;
            $scope.p_all_page = Math.ceil($scope.count / $scope.p_pernum);
            reloadPno();//第一次调用并加载
        });
    }

    //搜索
    $scope.mySea = true;//默认隐藏
    $scope.seaToggle = function () {
        $scope.mySea = !$scope.mySea;
    };
    //重置
    $scope.resetBtn = function () {
        $scope.consignorId =null;
        $scope.billCode =null;
        $scope.payeeStatus =null;
        $scope.billDateStart =null;
        $scope.billDateEnd =null;
        $(".valName").val("");
    };

    //单选按钮选中
    $scope.select = function (id) {
        //alert(id);
    };
    //首页
    $scope.p_index = function (e) {
        if (e.p_current == 1) {
            return;
        } else {
            $scope.load_page(1);
        }
    };
    //尾页
    $scope.p_last = function (e) {
        if (e.p_current == $scope.p_all_page) {
            return;
        }
        $scope.load_page($scope.p_all_page);
    };
    //加载某一页
    $scope.load_page = function (page) {
        getPage(page, $scope.p_pernum, "", "");
    };
    //上一页
    $scope.prev_page = function (e) {
        if (e.p_current == 0) {
            return;
        }
        getPage(e.p_current - 1, $scope.p_pernum, "", "");
    };
    //下一页
    $scope.next_page = function (e) {
        if (e.p_current == $scope.p_all_page) {
            return;
        }
        getPage(e.p_current + 1, $scope.p_pernum, "", "");
    };
    //跳转到指定页
    $scope.goBtn = function () {
        var idx = $("#on").val();
        getPage(idx, $scope.p_pernum, "", "");
    };
    //初始化页码
    var reloadPno = function () {
        $scope.pages = calculateIndexes($scope.p_current, $scope.p_all_page, 8);
    };
    var calculateIndexes = function (current, length, displayLength) {
        var indexes = [];
        var start = Math.round(current - displayLength / 2);
        var end = Math.round(current + displayLength / 2);
        if (start <= 1) {
            start = 1;
            end = start + displayLength - 1;
            if (end >= length - 1) {
                end = length - 1;
            }
        }
        if (end >= length - 1) {
            end = length;
            start = end - displayLength + 1;
            if (start <= 1) {
                start = 1;
            }
        }
        for (var i = start; i <= end; i++) {
            indexes.push(i);
        }
        return indexes;
    };

    //账单状态
    $scope.selectOptions = [
        {
            id: 10,
            name: "未收款"
        },
        {
            id: 20,
            name: "部分收款"
        },
        {
            id: 30,
            name: "收款完成"
        }
    ];
    //获取开始日期
    $scope.getTime=function($event,str){
        $scope.billDateStart =$scope.billDateStart == null ? getNowFormatDate() : $scope.billDateStart;
        layui.laydate({
            elem:$event.target,
            istime: false,//是否开启时间选择
            istoday: true,//是否显示今天
            isclear: true,//是否显示清空
            festival: true, //显示节日
            format:'YYYY-MM-DD',//日期格式
            choose:function(date){
                $scope[str]=date;
            }
        })
    };
    //获取截取日期
    $scope.getTimeEnd=function($event,str){
        $scope.billDateEnd =$scope.billDateEnd == null ? getNowFormatDate() : $scope.billDateEnd;
        layui.laydate({
            elem:$event.target,
            istime: false,//是否开启时间选择
            istoday: true,//是否显示今天
            isclear: true,//是否显示清空
            festival: true, //显示节日
            format:'YYYY-MM-DD',//日期格式
            choose:function(date){
                $scope[str]=date;
            }
        })
    };
    //获取当前的时间
    function getNowFormatDate() {
        var date = new Date();
        var seperator1 = "-";
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate;
        return currentdate;
    };
    // 导出
    $scope.export = function () {
        var arr = [];
        var proceedList = $("input[name='id_name']:checked");
        var proceedVal = proceedList.val();
        if (!proceedVal) {
            layer.msg('请选择账单');
        } else {
            if (proceedList.length > 1) {
                for (var i = 0; i < proceedList.length; i++) {
                    var checkVal = $(proceedList[i]).val();
                    arr.push(parseInt(checkVal))
                }
            } else {
                for (var i = 0; i < proceedList.length; i++) {
                    var checkVal = $(proceedList[i]).val();
                    arr.push(parseInt(checkVal))
                }
            }
            window.location="receivable/export-proceed?orderIds="+arr;
        }

    };
}]);